package net.hekka.canyoutalk.web.processor;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Hashtable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.NamingException;
import net.hekka.canyoutalk.data.constant.ConnectionConstant;
import net.hekka.canyoutalk.data.executor.TempLogQuerySqlExecutor;
import net.hekka.canyoutalk.data.vo.TempLogQueryVo;
import net.hekka.canyoutalk.data.vo.list.TempLogQueryVoList;
import net.hekka.canyoutalk.share.database.DataSourceConnector;
import net.hekka.canyoutalk.share.property.RequestParameterProperties;
import net.hekka.canyoutalk.web.util.PageNavigator;
import net.hekka.canyoutalk.web.vo.LogAggregationVo;
import net.hekka.canyoutalk.web.vo.LogAggregationVoList;
import net.hekka.canyoutalk.web.vo.PageVo;

/**
 * 콘텐츠 업로드의 상태 정보를 위한 프로세서 이다.
 * @author Jeffrey Ha
 */
public class TempLogQueryProcessor {

    private boolean test = false;

    /**
     * 생성자
     */
    public TempLogQueryProcessor() {
    }

    /**
     * 테스트 생성자
     * @param test true면 테스트 파일
     */
    public TempLogQueryProcessor(boolean test) {
        this.test = test;
    }

    /**
     * NormalView 해당하는 정보를 가져온다.
     * @param requestParameterProperties request parameter정보를 가지고 있는 requestParameterProperties
     * @return NormalView 해당하는 정보
     */
    public Hashtable getNormalView(RequestParameterProperties requestParameterProperties) {
        Connection dbConnection = null;
        Hashtable hashtable = new Hashtable();

        try {
            dbConnection = getConnection();
            int page = Integer.parseInt(requestParameterProperties.getProperty("nowPage"));
            int queryCount = Integer.parseInt(requestParameterProperties.getProperty("queryCount"));
            int totalCount = Integer.parseInt(requestParameterProperties.getProperty("totalCount"));
            int startRow = (page - 1) * queryCount;
            PageNavigator pageNavigator = new PageNavigator(page, totalCount, queryCount, (totalCount / queryCount) + 1);
            PageVo pageVo = setPageVo(pageNavigator);
            TempLogQuerySqlExecutor tempLogQuerySqlExecutor = new TempLogQuerySqlExecutor();
            TempLogQueryVoList tempLogQueryVoList = tempLogQuerySqlExecutor.getTempLogQueryVoListForNormalView(dbConnection, startRow, queryCount);
            hashtable.put("tempLogQueryVoList", tempLogQueryVoList);
            hashtable.put("pageVo", pageVo);

        } catch (SQLException ex) {
            Logger.getLogger(TempLogQueryProcessor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NamingException ex) {
            Logger.getLogger(TempLogQueryProcessor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(TempLogQueryProcessor.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            cleanup(dbConnection);
            return hashtable;
        }

    }

    /**
     * DetailView 해당하는 정보를 가져온다.
     * @param requestParameterProperties request parameter정보를 가지고 있는 requestParameterProperties
     * @return DetailView 해당하는 정보
     */
    public Hashtable getDetailView(RequestParameterProperties requestParameterProperties) {
        Connection dbConnection = null;
        Hashtable hashtable = new Hashtable();

        try {
            dbConnection = getConnection();
            int page = Integer.parseInt(requestParameterProperties.getProperty("nowPage"));
            int queryCount = Integer.parseInt(requestParameterProperties.getProperty("queryCount"));
            int totalCount = Integer.parseInt(requestParameterProperties.getProperty("totalCount"));
            int startRow = (page - 1) * queryCount;
            PageNavigator pageNavigator = new PageNavigator(page, totalCount, queryCount, (totalCount / queryCount) + 1);
            PageVo pageVo = setPageVo(pageNavigator);
            TempLogQuerySqlExecutor tempLogQuerySqlExecutor = new TempLogQuerySqlExecutor();
            TempLogQueryVoList tempLogQueryVoList = tempLogQuerySqlExecutor.getTempLogQueryVoListForDetailView(dbConnection, startRow, queryCount);
            hashtable.put("tempLogQueryVoList", tempLogQueryVoList);
            hashtable.put("pageVo", pageVo);

        } catch (SQLException ex) {
            Logger.getLogger(TempLogQueryProcessor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NamingException ex) {
            Logger.getLogger(TempLogQueryProcessor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(TempLogQueryProcessor.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            cleanup(dbConnection);
            return hashtable;
        }

    }

    /**
     * StatusChangeList 해당하는 정보를 가져온다.
     * @param requestParameterProperties request parameter정보를 가지고 있는 requestParameterProperties
     * @return StatusChangeList 해당하는 정보
     */
    public Hashtable getStatusChangeList(RequestParameterProperties requestParameterProperties) {
        Connection dbConnection = null;
        Hashtable hashtable = new Hashtable();

        try {
            dbConnection = getConnection();
            int page = Integer.parseInt(requestParameterProperties.getProperty("nowPage"));
            int queryCount = Integer.parseInt(requestParameterProperties.getProperty("queryCount"));
            int totalCount = Integer.parseInt(requestParameterProperties.getProperty("totalCount"));
            int startRow = (page - 1) * queryCount;
            PageNavigator pageNavigator = new PageNavigator(page, totalCount, queryCount, (totalCount / queryCount) + 1);
            PageVo pageVo = setPageVo(pageNavigator);
            TempLogQuerySqlExecutor tempLogQuerySqlExecutor = new TempLogQuerySqlExecutor();
            TempLogQueryVoList tempLogQueryVoList = tempLogQuerySqlExecutor.getTempLogQueryVoListForStatusChangeList(dbConnection, startRow, queryCount);
            hashtable.put("tempLogQueryVoList", tempLogQueryVoList);
            hashtable.put("pageVo", pageVo);
        } catch (SQLException ex) {
            Logger.getLogger(TempLogQueryProcessor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NamingException ex) {
            Logger.getLogger(TempLogQueryProcessor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(TempLogQueryProcessor.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            cleanup(dbConnection);
            return hashtable;
        }
    }

    /**
     * BroadcastList 해당하는 정보를 가져온다.
     * @param requestParameterProperties request parameter정보를 가지고 있는 requestParameterProperties
     * @return BroadcastList 해당하는 정보
     */
    public TempLogQueryVoList getBroadcastList(RequestParameterProperties requestParameterProperties) {
        Connection dbConnection = null;
        TempLogQueryVoList tempLogQueryVoList = null;

        try {
            dbConnection = getConnection();
            int sequence = Integer.parseInt(requestParameterProperties.getProperty("sequence01"));
            TempLogQuerySqlExecutor tempLogQuerySqlExecutor = new TempLogQuerySqlExecutor();
            tempLogQueryVoList = tempLogQuerySqlExecutor.getTempLogQueryVoListForBroadcastList(dbConnection, sequence);

        } catch (SQLException ex) {
            Logger.getLogger(TempLogQueryProcessor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NamingException ex) {
            Logger.getLogger(TempLogQueryProcessor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(TempLogQueryProcessor.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            cleanup(dbConnection);
            return tempLogQueryVoList;
        }

    }

    /**
     * FriendStateList 해당하는 정보를 가져온다.
     * @param requestParameterProperties request parameter정보를 가지고 있는 requestParameterProperties
     * @return FriendStateList 해당하는 정보
     */
    public TempLogQueryVoList getFriendStateList(RequestParameterProperties requestParameterProperties) {
        Connection dbConnection = null;
        TempLogQueryVoList tempLogQueryVoList = null;

        try {
            dbConnection = getConnection();
            int sequence = Integer.parseInt(requestParameterProperties.getProperty("sequence01"));
            TempLogQuerySqlExecutor tempLogQuerySqlExecutor = new TempLogQuerySqlExecutor();
            tempLogQueryVoList = tempLogQuerySqlExecutor.getTempLogQueryVoListForFriendStateList(dbConnection, sequence);

        } catch (SQLException ex) {
            Logger.getLogger(TempLogQueryProcessor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NamingException ex) {
            Logger.getLogger(TempLogQueryProcessor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(TempLogQueryProcessor.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            cleanup(dbConnection);
            return tempLogQueryVoList;
        }

    }

    public Hashtable getDetailLogAggregationList(RequestParameterProperties requestParameterProperties) {
        Connection dbConnection = null;
        Hashtable hashtable = new Hashtable();

        try {
            System.out.println("[TempLogQueryProcessor] getDetailLogAggregationList called...");

            dbConnection = getConnection();

            int page = Integer.parseInt(requestParameterProperties.getProperty("nowPage"));
            int queryCount = Integer.parseInt(requestParameterProperties.getProperty("queryCount"));
            int totalCount = Integer.parseInt(requestParameterProperties.getProperty("totalCount"));
            int startRow = (page - 1) * queryCount;

            // 페이지 관리
            PageNavigator pageNavigator = new PageNavigator(page, totalCount, queryCount, (totalCount / queryCount) + 1);
            PageVo pageVo = setPageVo(pageNavigator);

            // 상태변경 리스트를 구한다.
            TempLogQuerySqlExecutor tempLogQuerySqlExecutor = new TempLogQuerySqlExecutor();
            TempLogQueryVoList tempLogQueryVoList = tempLogQuerySqlExecutor.getTempLogQueryVoListForDetailView(dbConnection, startRow, queryCount);

            if (tempLogQueryVoList != null) {
                LogAggregationVoList logAggregationVoList = new LogAggregationVoList();

                System.out.println("### tempLogQueryVoList.size() : " + tempLogQueryVoList.size());

                // 상태변경 리스트에 해당하는 친구상태 리스트를 구한다.
                for (int i = 0; i < tempLogQueryVoList.size(); i++) {
                    // LogAggregationVo 생성
                    LogAggregationVo logAggregationVo = new LogAggregationVo();

                    // Key Value Object 를 구한다
                    TempLogQueryVo tempLogQueryVo = tempLogQueryVoList.get(i);
                    // set Key Value Object
                    logAggregationVo.setKeyLogQueryVo(tempLogQueryVo);

                    // Key에 해당하는 List를 구한다.
                    int sequence = tempLogQueryVo.getSequence();

                    TempLogQueryVoList tempLogQueryVoList2 = tempLogQuerySqlExecutor.getTempLogQueryVoListForBroadcastList(dbConnection, sequence);
                    // set List
                    logAggregationVo.setLogQueryVoList(tempLogQueryVoList2);

                    // LogAggregationVoList에 추가
                    logAggregationVoList.add(logAggregationVo);
                }
                System.out.println("### logAggregationVoList.size(): " + logAggregationVoList.size());
                hashtable.put("logAggregationVoList", logAggregationVoList);
            }

            hashtable.put("pageVo", pageVo);

        } catch (SQLException ex) {
            Logger.getLogger(TempLogQueryProcessor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NamingException ex) {
            Logger.getLogger(TempLogQueryProcessor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(TempLogQueryProcessor.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            cleanup(dbConnection);
            return hashtable;
        }

    }

    public Hashtable getMyFriendAggregationList(RequestParameterProperties requestParameterProperties) {
        Connection dbConnection = null;
        Hashtable hashtable = new Hashtable();

        try {
            System.out.println("[TempLogQueryProcessor] getMyFriendAggregationList called...");

            dbConnection = getConnection();

            int page = Integer.parseInt(requestParameterProperties.getProperty("nowPage"));
            int queryCount = Integer.parseInt(requestParameterProperties.getProperty("queryCount"));
            int totalCount = Integer.parseInt(requestParameterProperties.getProperty("totalCount"));
            int startRow = (page - 1) * queryCount;

            // 페이지 관리
            PageNavigator pageNavigator = new PageNavigator(page, totalCount, queryCount, (totalCount / queryCount) + 1);
            PageVo pageVo = setPageVo(pageNavigator);

            // 상태변경 리스트를 구한다.
            TempLogQuerySqlExecutor tempLogQuerySqlExecutor = new TempLogQuerySqlExecutor();
            TempLogQueryVoList tempLogQueryVoList = tempLogQuerySqlExecutor.getTempLogQueryVoListForStatusChangeList(dbConnection, startRow, queryCount);

            if (tempLogQueryVoList != null) {
                LogAggregationVoList logAggregationVoList = new LogAggregationVoList();

                System.out.println("### tempLogQueryVoList.size() : " + tempLogQueryVoList.size());

                // 상태변경 리스트에 해당하는 친구상태 리스트를 구한다.
                for (int i = 0; i < tempLogQueryVoList.size(); i++) {
                    // LogAggregationVo 생성
                    LogAggregationVo logAggregationVo = new LogAggregationVo();
                    
                    // Key Value Object 를 구한다
                    TempLogQueryVo tempLogQueryVo = tempLogQueryVoList.get(i);
                    // set Key Value Object
                    logAggregationVo.setKeyLogQueryVo(tempLogQueryVo);

                    // Key에 해당하는 List를 구한다.
                    int sequence = tempLogQueryVo.getSequence();

                    TempLogQueryVoList tempLogQueryVoList2 = tempLogQuerySqlExecutor.getTempLogQueryVoListForMyAllFriendStateList(dbConnection, sequence);
                    // set List
                    logAggregationVo.setLogQueryVoList(tempLogQueryVoList2);

                    // LogAggregationVoList에 추가
                    logAggregationVoList.add(logAggregationVo);
                }
                System.out.println("### logAggregationVoList.size(): " + logAggregationVoList.size());
                hashtable.put("logAggregationVoList", logAggregationVoList);
            }

            hashtable.put("pageVo", pageVo);
            
        } catch (SQLException ex) {
            Logger.getLogger(TempLogQueryProcessor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NamingException ex) {
            Logger.getLogger(TempLogQueryProcessor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(TempLogQueryProcessor.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            cleanup(dbConnection);
            return hashtable;
        }

    }

    public Hashtable getBroadcastLogAggregationList(RequestParameterProperties requestParameterProperties) {
        Connection dbConnection = null;
        Hashtable hashtable = new Hashtable();

        try {
            dbConnection = getConnection();

            int page = Integer.parseInt(requestParameterProperties.getProperty("nowPage"));
            int queryCount = Integer.parseInt(requestParameterProperties.getProperty("queryCount"));
            int totalCount = Integer.parseInt(requestParameterProperties.getProperty("totalCount"));
            int startRow = (page - 1) * queryCount;

            // 페이지 관리
            PageNavigator pageNavigator = new PageNavigator(page, totalCount, queryCount, (totalCount / queryCount) + 1);
            PageVo pageVo = setPageVo(pageNavigator);

            // 상태변경 리스트를 구한다.
            TempLogQuerySqlExecutor tempLogQuerySqlExecutor = new TempLogQuerySqlExecutor();
            TempLogQueryVoList tempLogQueryVoList = tempLogQuerySqlExecutor.getTempLogQueryVoListForStatusChangeList(dbConnection, startRow, queryCount);

            if (tempLogQueryVoList != null) {
                LogAggregationVoList logAggregationVoList = new LogAggregationVoList();

                // 상태변경 리스트에 해당하는 친구상태 리스트를 구한다.
                for (int i = 0; i < tempLogQueryVoList.size(); i++) {
                    // Key Value Object 를 구한다
                    TempLogQueryVo tempLogQueryVo = tempLogQueryVoList.get(i);

                    // Key에 해당하는 List를 구한다.
                    int sequence = tempLogQueryVo.getSequence();
                    tempLogQueryVoList = tempLogQuerySqlExecutor.getTempLogQueryVoListForFriendStateList(dbConnection, sequence);

                    // LogAggregationVo 생성
                    LogAggregationVo logAggregationVo = new LogAggregationVo();
                    logAggregationVo.setKeyLogQueryVo(tempLogQueryVo);
                    logAggregationVo.setLogQueryVoList(tempLogQueryVoList);

                    // LogAggregationVoList에 추가
                    logAggregationVoList.add(logAggregationVo);

                }
                hashtable.put("logAggregationVoList", logAggregationVoList);
            }
            
            hashtable.put("pageVo", pageVo);

        } catch (SQLException ex) {
            Logger.getLogger(TempLogQueryProcessor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NamingException ex) {
            Logger.getLogger(TempLogQueryProcessor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(TempLogQueryProcessor.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            cleanup(dbConnection);
            return hashtable;
        }

    }

    /**
     * NormalView 전체 수를 구한다.
     * @return NormalView 전체 수
     */
    public int getTotalCountOfNormalView() {
        Connection dbConnection = null;
        int totalCount = 0;

        try {
            dbConnection = getConnection();
            TempLogQuerySqlExecutor tempLogQuerySqlExecutor = new TempLogQuerySqlExecutor();
            totalCount = tempLogQuerySqlExecutor.getTotalCountForNormalView(dbConnection);

        } catch (SQLException ex) {
            Logger.getLogger(TempLogQueryProcessor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NamingException ex) {
            Logger.getLogger(TempLogQueryProcessor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(TempLogQueryProcessor.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            cleanup(dbConnection);
            return totalCount;
        }
    }

    /**
     * DetailView 전체 수를 구한다.
     * @return DetailView 전체 수
     */
    public int getTotalCountOfDetailView() {
        Connection dbConnection = null;
        int totalCount = 0;

        try {
            dbConnection = getConnection();
            TempLogQuerySqlExecutor tempLogQuerySqlExecutor = new TempLogQuerySqlExecutor();
            totalCount = tempLogQuerySqlExecutor.getTotalCountForDetailView(dbConnection);
            //        }
        } catch (SQLException ex) {
            Logger.getLogger(TempLogQueryProcessor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NamingException ex) {
            Logger.getLogger(TempLogQueryProcessor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(TempLogQueryProcessor.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            cleanup(dbConnection);
            return totalCount;
        }

    }

    /**
     * StatusChange 전체 수를 구한다.
     * @return StatusChange 전체 수
     */
    public int getTotalCountOfStatusChange() {
        Connection dbConnection = null;
        int totalCount = 0;

        try {
            dbConnection = getConnection();
            TempLogQuerySqlExecutor tempLogQuerySqlExecutor = new TempLogQuerySqlExecutor();
            totalCount = tempLogQuerySqlExecutor.getTotalCountForStatusChange(dbConnection);

        } catch (SQLException ex) {
            Logger.getLogger(TempLogQueryProcessor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NamingException ex) {
            Logger.getLogger(TempLogQueryProcessor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(TempLogQueryProcessor.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            cleanup(dbConnection);
            return totalCount;
        }
    }

    private Connection getConnection() throws SQLException, NamingException, Exception {
        Connection dbConnection = null;
        if (test) {
            dbConnection = DatabaseTestConnector.getConnection();
        } else {
            dbConnection = DataSourceConnector.getConnection(ConnectionConstant.databaseType);
        }
        return dbConnection;
    }

    private void cleanup(Connection con) {
        DataSourceConnector.cleanup(con);
    }

    private PageVo setPageVo(PageNavigator _pageNavigator) {
        PageNavigator pageNavigator = _pageNavigator;
        PageVo pageVo = new PageVo();
        pageVo.setPageIsPrev(pageNavigator.isPrevPage());
        pageVo.setPageIsNext(pageNavigator.isNextPage());
        pageVo.setPageStartNumber(pageNavigator.getStartPage());
        pageVo.setPageEndNumber(pageNavigator.getEndPage());
        return pageVo;
    }
}
